<?php
	/**
	 * 	教师登录
	 * 		@return $teacher[tname,tpass,coursename]
	 */
	function selectTeaByNameAndPass($tname,$tnumber){
		$conn = mysqli_connect("localhost:3306","mebin","171110301cwb","sms");
		$sql =	" select " .
				" 	tname,tnumber,tcourse,tid,collegename,tattr " .
				" from " .
				" 	teacher,college" .
				" where teacher.tcollegeid=college.collegeid and" .
				" 	tname='$tname' and tnumber='$tnumber' " ;
		$result = mysqli_query($conn, $sql);
		$teacher = null;
		if(mysqli_num_rows($result) == 1){
			$rows = mysqli_fetch_assoc($result);
			$teacher['tname'] = $rows['tname'];
			$teacher['tnumber'] = $rows['tnumber'];
			$teacher['tcourse'] = $rows['tcourse'];
			$teacher['tid'] = $rows['tid'];
			$teacher['tattr'] = $rows['tattr'];
			$teacher['collegename'] = $rows['collegename'];
		}
		mysqli_close($conn);
		return $teacher;
	}

	/**
	 * 	查询学生选课具体信息
	 * 	@return $stusecs 
	 */
	function selectStusec(){
		$conn = mysqli_connect("localhost:3306","mebin","171110301cwb","SMS");
		$sql =  " select stu_sec_spepul_id,sections,spepul_sections " .
				"     from student_section_spepul sp left join student_section sr " .
				" on sp.stu_sec_spepul_id = sr.stu_secid ";
		$result = mysqli_query($conn, $sql);
		$stusecs = [];
		while($rows = mysqli_fetch_assoc($result)){
			$stusecs[] = $rows;
		}
		mysqli_close($conn);
		return $stusecs;
	}
	
	/**
	 * 查询选某门课的学生具体信息
	 * 	@return $students
	 */
	function selectStusecInfor($stuid){
		$conn = mysqli_connect("localhost:3306","mebin","171110301cwb","SMS");
		$sql = 	" select " .
				" 	stuid,sname,snumber,collegename,gradename,classname " .
				" from " .
				" 	student_roll sr left join college c " .
				" 	  on sr.scollegeid = c.collegeid " .
				" 	left join grade g " .
				" 	  on sr.sgradeid = g.gradeid " .
				" 	left join class c1 " .
				" 	  on sr.sclassid = c1.classid " .
				" where	sr.stuid = '{$stuid}' " ;
		$result = mysqli_query($conn, $sql);
		$students = null;
		if(mysqli_num_rows($result) == 1){
			$rows = mysqli_fetch_assoc($result);
			$students['stuid'] = $rows['stuid'];
			$students['sname'] = $rows['sname'];
			$students['snumber'] = $rows['snumber'];
			$students['collegename'] = $rows['collegename'];
			$students['gradename'] = $rows['gradename'];
			$students['classname'] = $rows['classname'];
		}
		mysqli_close($conn);
		return $students;
	}
	
	/**
	 * 输入学生的成绩
	 */
	function insertStumark($stumark){
		$conn = mysqli_connect("localhost:3306","mebin","171110301cwb","SMS");
		$flag = true;
		$sql = "select mark_tid from mark where mark_tid='{$stumark["mark_tid"]}' ";
		$result = mysqli_query($conn,$sql);
		if(mysqli_num_rows($result)){
			$sql =  " update mark set " .
					"    mark_stuid='{$stumark["mark_stuid"]}',mark='{$stumark["mark"]}' " .
					" where mark_tid='{$stumark["mark_tid"]}' ";
			mysqli_query($conn,$sql);
			if(mysqli_errno($conn)){
				$flag = false;
				echo mysqli_error($conn);
			}
		}else{
			$sql = 	" insert into mark " .
					"  	(mark_tid,mark_stuid,mark) " .
					" values " .
					" 	('{$stumark["mark_tid"]}','{$stumark["mark_stuid"]}','{$stumark["mark"]}') " ;
			mysqli_query($conn,$sql);
			if(mysqli_errno($conn)){
				$flag = false;
				echo mysqli_error($conn);
			}
		}
		mysqli_close($conn);
		return $flag;
	}
	
	/**
	 * 根据tid查询学生成绩信息
	 */
	function selectStumarkBytid($tid){
		$conn = mysqli_connect("localhost:3306","mebin","171110301cwb","SMS");
		$sql = " select mark_stuid,mark from mark where mark_tid = $tid ";
		$result = mysqli_query($conn,$sql);
		$marks = null;
		if(mysqli_num_rows($result) == 1){
			$rows = mysqli_fetch_assoc($result);
			$marks["mark"] = explode(',',$rows["mark"]);
			$marks["mark_stuid"] = explode(',',$rows["mark_stuid"]);
		}
		mysqli_close($conn);
		return $marks;
	}
	
?> 